<?php
  /**
   * @author EVANDRO DE OLIVEIRA.
   * @category MODEL
   * @package MODELS.
   * @since 05/05/2012.
   * @last_update 05/05/2012.
   * @copyright 2012.
   * @version 1.0.
   *
   */
  class Application_Model_Bairros extends Zend_Db_Table_Abstract{
    const IND_ATIVO = 1;
    const IND_INATIVO = 0;
    
    /**
     * Define a tabela padrão da controller
     */
    protected function _setup(){
      $this->_name = 'bairros';
      parent::_setup();
    } 
    
    /**
     * Retorna todos os registros ativos.
     */
    public function getAtivos(){
      $strSql = $this->select()->where($this->_name . '.st_bairro = ?', self::IND_ATIVO);
      
      $objResult = $this->fetchAll($strSql);
            
      return Core_Commons::onLoad($objResult);
    }
    
    public function getByCep($intCep){
    	$arrColsBairro = array($this->_name . '.idBairro', $this->_name . '.ds_bairro');
    	$arrColsLogradouro = array();
    	
      $strSql = $this->getAdapter()
                     ->select()
                     ->from($this->_name, $arrColsBairro)
                     ->join('logradouros', 'logradouros.idBairro = ' . $this->_name . '.idBairro', $arrColsLogradouro)
                     ->where('logradouros.ds_cep = ?', $intCep)
                     ->where($this->_name . '.st_bairro = ?', self::IND_ATIVO);
      
      $objResult = $this->getAdapter()->fetchRow($strSql);
            
      return Core_Commons::onLoad($objResult);
    }
  }